package  com.autel;
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.SimpleAuthenticationDetailsProvider;
import com.oracle.bmc.auth.SimplePrivateKeySupplier;
import com.oracle.bmc.auth.StringPrivateKeySupplier;
import com.oracle.bmc.objectstorage.ObjectStorage;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.model.CreateBucketDetails;
import com.oracle.bmc.objectstorage.model.CreatePreauthenticatedRequestDetails;
import com.oracle.bmc.objectstorage.model.PreauthenticatedRequest;
import com.oracle.bmc.objectstorage.requests.*;
import com.oracle.bmc.objectstorage.responses.CreatePreauthenticatedRequestResponse;
import com.oracle.bmc.objectstorage.responses.DeleteObjectResponse;
import com.oracle.bmc.objectstorage.responses.GetBucketResponse;
import com.oracle.bmc.objectstorage.responses.GetNamespaceResponse;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.oracle.bmc.Region.*;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main(String[] args) throws Exception {
        final String compartmentId = "ocid1.compartment.oc1..aaaaaaaafwngkdeaj6lbd6ilyhrjh6fxnrit2udjv43sxkux4eipn52wbj2q";
        final String bucket = "default";
        final String objectName = "test";
        final String tenantId = "ocid1.tenancy.oc1..aaaaaaaaylwoa3nd5c4kerfueyqykf4trwvtpxpupgtcfndxq4dp54rvbbmq";
        final String userId = "ocid1.user.oc1..aaaaaaaaztvtwb4sempwrmuzq4qynjjk4dg2gkcvr37trkx7dw2dvrsq7l7q";
        final String fingerprint = "d8:1d:1e:94:71:23:c4:15:a5:6f:6c:1d:2c:3a:07:3c";
        final String passPhrase = "Gao@2022";

        final AuthenticationDetailsProvider provider =  SimpleAuthenticationDetailsProvider.builder()
                .tenantId(tenantId)
                .userId(userId)
                .fingerprint(fingerprint)
                .privateKeySupplier(()->App.class.getResourceAsStream("/api.pem"))
                .passPhrase(passPhrase)
                .region(US_PHOENIX_1)
                .build();


        ObjectStorage client = new ObjectStorageClient(provider);
        // namespace
        GetNamespaceResponse namespaceResponse =
                client.getNamespace(GetNamespaceRequest.builder().build());
        String namespaceName = namespaceResponse.getValue();
        System.out.println(namespaceName);

//        PutObjectRequest putObjectRequest =
//                PutObjectRequest.builder()
//                        .namespaceName(namespaceName)
//                        .bucketName(bucket)
//                        .objectName(objectName)
//                        .putObjectBody(
//                                new ByteArrayInputStream("data".getBytes(StandardCharsets.UTF_8)))
//                        .build();
//        client.putObject(putObjectRequest);
//
//
//                // 删除文件
//        DeleteObjectResponse putResponse =
//                client.deleteObject(DeleteObjectRequest.builder()
//                                .namespaceName(namespaceName)
//                        .bucketName(bucket)
//                        .objectName(objectName).build());
//
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        CreatePreauthenticatedRequestDetails createPreauthenticatedRequestDetails = CreatePreauthenticatedRequestDetails.builder()
                .name("EXAMPLE-name-Value")
                .objectName(objectName)
                .accessType(CreatePreauthenticatedRequestDetails.AccessType.ObjectRead)
                .timeExpires(sdf.parse("2023-03-03 00:00:00")).build();

        CreatePreauthenticatedRequestRequest createPreauthenticatedRequestRequest = CreatePreauthenticatedRequestRequest.builder()
                .namespaceName(namespaceName)
                .bucketName(bucket)
                .createPreauthenticatedRequestDetails(createPreauthenticatedRequestDetails)
                .build();

        /* Send request to the Client */
        CreatePreauthenticatedRequestResponse response = client.createPreauthenticatedRequest(createPreauthenticatedRequestRequest);
        System.out.println(response);
    }
}
